<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/public/header.jsp"%>
<%
	String callback = request.getParameter("callback");
	String isShow = request.getParameter("isShow") == null ? "" : request.getParameter("isShow");
	String apply_id = request.getParameter("apply_id") == null ? "" : request.getParameter("apply_id");
	String return_id = request.getParameter("return_id") == null ? "" : request.getParameter("return_id");
	String business_type = request.getParameter("business_type") == null ? "" : request.getParameter("business_type");
	Map<String, Object> sessionMap = (Map<String, Object>) session.getAttribute("LOGIN_USER");
	String person_id = String.valueOf(sessionMap.get("id"));
	String person = String.valueOf(sessionMap.get("truename"));
	boolean isAdd = "".equals(return_id);
    int flag = 0;
    if(!isAdd){
    	flag = 1;
    }
%>
<html>
	<head>
		<title>工单领料退料</title>
		<script type="text/javascript" src="<%=contextPath %>/public/js/extend.js"></script>
		<style>
			.td1{
				padding-top:10px;
				padding-bottom:5px;
				font-size: 12px;
				background-color: #c0ebd7;
				width:20%;
			}
			.td2{
				padding:5px;
			}
		</style>
	<script type="text/javascript">
		var lastIndex;
		$(function(){
			$("#mSearchValue").keypress(function(e) { 
		    	// 回车键事件 
		     	if(e.which == 13) { 
		     		 domSearch();
		       	} 
		   	}); 
			$("#material_type_id").combotree({
				url:'<%=contextPath %>/springMVC/group/getSystemTypeTree?type=0',
				method:'POST',
				animate:true,
				lines:true,
				onChange:function(){
					domSearch();
				}
			});
			$('#mtb').hide();
			<%if(isAdd){%>
			$("#operation_date").datetimebox('setValue',getTodaySecond());
			$.ajax({
				type:'POST',
				url:'<%=contextPath %>/springMVC/warehouse/getGDJgByApplyId?apply_id=<%=apply_id%>&business_type=<%=business_type%>',
				success:function(rtJson){
					if(rtJson.rtState == '0'){
						$('#gd_code').val(rtJson.rtData.gd_code);	
						$('#gd_name').val(rtJson.rtData.gd_name);	
						$('#dept_name').val(rtJson.rtData.gd_dept_name);	
						$('#dept_id').val(rtJson.rtData.gd_dept_id);	
						$('#gd_id').val(rtJson.rtData.gd_id);	
					}
				}
			});
			
		<%}else{%>
				$('#ff').form('load','<%=contextPath %>/springMVC/gd/getReturnApplyById?return_id=<%=return_id%>');
			<%}%>
			
				$('#table').datagrid({
					<%=isAdd?"url:'"+contextPath+"/springMVC/gd/listGDJgApplyDetail?apply_id="+apply_id+"&business_type="+business_type+"',":"url:'"+contextPath+"/springMVC/gd/listGDReturnDetail?return_id="+return_id+"&business_type="+business_type+"',"%>
					rownumbers:true,
					fitColumns:true,
					frozenColumns:[[
						{field:'ck',checkbox:true},
					    {title:'产品id',field:'material_id',hidden:true}
					]],
					columns:[[
						{title:'产品编号',field:'code',width:100,align:'center'},
						{title:'产品名称',field:'name',width:200,align:'center'},
						{title:'规格/型号',field:'format_type',width:200,align:'center'},
						{title:'单位',field:'unit_name',width:50,align:'center'},
						{title:'领用数量',field:'apply_num',width:100,align:'center'},
						{title:'退料数量',field:'return_num',width:100,align:'center',formatter:function(value,row,index){
							if(value == undefined){
								row.return_num = parseFloat(row.apply_num);
								return row.return_num;
							}
							return value;
						},editor:{
							type:'numberbox',
							options:{
								min:0,
								precision:2
							}
						}}
					]],
					onBeforeLoad:function(){
						$(this).datagrid('rejectChanges');
					},
					onClickCell:function(rowIndex,field,value){
						if (lastIndex != rowIndex){
							$('#table').datagrid('endEdit', lastIndex);
							$('#table').datagrid('beginEdit', rowIndex);
							var ed = $('#table').datagrid('getEditor', {index:rowIndex,field:field});
							if(ed == null){
								ed = $('#table').datagrid('getEditor', {index:rowIndex,field:'num'});
							}
							$(ed.target).select();
						}
						lastIndex = rowIndex;
					},
					toolbar:[
						{
						id:'save',
						text:'保存修改',
						disabled:false,
						iconCls:'icon-save',
						handler:function(){
							$('#table').datagrid('acceptChanges');
							lastIndex =-1;
						}
						},'-',{
						id:'delete',
						text:'删除',
						disabled:false,
						iconCls:'icon-remove',
						handler:function(){
							$('#table').datagrid('acceptChanges');
							lastIndex =-1;
							var rows = $('#table').datagrid('getSelections');
							if (rows.length > 0){
								for(var i=0;i<rows.length;i++){
									var row = rows[i];
									var index = $('#table').datagrid('getRowIndex', row);
									$('#table').datagrid('deleteRow', index);
								}
							}
							var data = $('#table').datagrid('getData');
							$('#table').datagrid('loadData',data);
						}
					}
				   ]
				});
		});
		
		function addGDJgReturn(obj){
			//自动提交数据
			$('#table').datagrid('acceptChanges');
			lastIndex =-1;
			//自动提交数据
			if($('#ff').form('validate')){
				var products="";
				var jsondata=$("#table").datagrid("getData");
				for(var i=0;i<jsondata.total;i++){
					if(products!=""){
						products+=";";
					}
					products+=jsonToStr(jsondata.rows[i]);
					var index = $("#table").datagrid("getRowIndex",jsondata.rows[i])+1;
					if(jsondata.rows[i].return_num == null || jsondata.rows[i].return_num <= 0){
						$.messager.alert('提示','第'+index+'行数量填写不正确！');
						return ;
					}else if(parseFloat(jsondata.rows[i].return_num) > parseFloat(jsondata.rows[i].apply_num)){
						$.messager.alert('提示','第'+index+'行数量不能大于到领用数量！');
						return ;
					}
				}
				if(!checkNullOREmpty(products,'产品为必填项')){return;}
				var operation_date = $("#operation_date").datetimebox('getValue');
				var gd_id = $('#gd_id').val();
				var gd_code = $('#gd_code').val();
				var gd_name = $('#gd_name').val();
				var dept_id = $('#dept_id').val();
				var dept_name = $('#dept_name').val();
				var remark = encodeURIComponent($('#remark').val());
				
				var param = "apply_id=<%=apply_id%>&person_id=<%=person_id%>&person=<%=person%>&operation_date="+operation_date+"&remark="+remark+"&gd_id="+gd_id+"&gd_code="+gd_code+"&gd_name="+gd_name+"&dept_id="+dept_id+"&dept_name="+dept_name+"&products="+products+"&business_type=<%=business_type%>";
				$(obj).hide();
				$.ajax({
					type:'POST',
					<%=isAdd?"url:'"+contextPath+"/springMVC/gd/addGDReturn',":"url:'"+contextPath+"/springMVC/gd/updateGDReturn?return_id="+return_id+"',"%>
					data:param,
					dataType:'json',
					success:function(rtJson){
						if(rtJson.rtState == '0'){
							eval('window.opener.<%=callback%>();');
							window.close();
						}
						else{
							$.messager.alert("提示",rtJson.rtMsrg);
						}
						$(obj).show();
					}
				});
			}
		}
		function domSearch()
		{
			var mSearchValue = $('#mSearchValue').val();
			var material_type_id = $('#material_type_id').combotree('getValue');
			$(combogridTarget).combogrid('grid').datagrid('load',{
				searchValue:mSearchValue,
				material_type_id:material_type_id
				});
		}
		function addRow(){
			$('#table').datagrid('endEdit', lastIndex);
			$('#table').datagrid('appendRow',{
				apply_num:'0'
			});
			var data = $('#table').datagrid('getData');
			$('#table').datagrid('loadData',data);
			lastIndex = $('#table').datagrid('getRows').length-1;
			$('#table').datagrid('selectRow', lastIndex);
			$('#table').datagrid('beginEdit', lastIndex);
		}
		function gdPrint(){
			openDialogResize("<%=contextPath%>/springViews/print/startSelfReturnPrint.jsp?return_id=<%=return_id%>&business_type=<%=business_type%>",1000, 800);
		}
	</script>
	</head>
	<body>
		<div style="width: 97%;padding-left:10px">
		<form id="ff">
			<table  style="border-collapse: collapse;" width="100%" border="1px" bordercolor="#0eb83a">
				<tr>
					<td align="right" class="td1" nowrap>工单编号：</td>
					<td class="td2" nowrap>
						<input type="hidden" id="gd_id" name="gd_id">
						<input  id="gd_code" name="gd_code" readOnly value="">
					</td>
					<td align="right" class="td1" nowrap>工单名称：</td>
					<td class="td2" nowrap>
						<input  id="gd_name" name="gd_name" readOnly value="">
					</td>
				</tr>
				<tr>
					<td align="right" class="td1" nowrap>申请部门：</td>
					<td class="td2" nowrap>
						<input  id="dept_name" name="dept_name" readOnly value="">
						<input type="hidden" id="dept_id" name="dept_id" >
					</td>
					<td align="right" class="td1" nowrap>申请人：</td>
					<td class="td2" nowrap>
						<input  id="person" name="person" readOnly value="<%=person%>">
					</td>
				</tr>
				<tr>
					<td align="right" class="td1" nowrap>申请日期：</td>
					<td class="td2" nowrap colspan='3'>
						<input  id="operation_date" name="operation_date" class="easyui-datetimebox" data-options="required:true">
					</td>
				</tr>
				<tr>
					<td align="right" class="td1" nowrap>摘要：</td>
					<td class="td2" nowrap colspan='3'>
						<textarea  id="remark" name="remark" rows="6" style="width: 100%"></textarea>
					</td>
				</tr>
			<%if(!"1".equals(isShow)){ %>
				<tr>
					<td align="right" class="td1" nowrap></td>
					<td class="td2" nowrap colspan='3'>
						<div align="left">
							<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-save',plain:false" onclick="addGDJgReturn(this);">提交</a>
							<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-cancel',plain:false" onclick="javascript:window.close();">关闭</a>
						</div>
					</td>
				</tr>
		<%} else{%>
							<tr>
					<td align="right" class="td1" nowrap></td>
					<td class="td2" nowrap colspan='3'>
						<div align="left">
							<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-print',plain:false" onclick="gdPrint();">打印预览</a>
						</div>
					</td>
				</tr>
							<%}%>
				<tr>
					<td colspan="4"  nowrap>
						<div id="table"></div>
					</td>
				</tr>
			</table>
			</form>
		</div>
	</body>
</html>